﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using TSqlServer;

/// <summary>
/// TCommon 的摘要说明
/// </summary>
public class TCommon
{
    //private const string wwwurl = "http://localhost/easycrm/";
    //private const string wwwurl = "http://211.144.35.131/easycrm/";
    public const string wwwurl = "http://211.144.35.250:8567/easycrm/";
    //public const string wwwurl = "http://172.16.21.162/easycrm/";
    public const string sendmailusers = "zhaibingying@cnap.com.cn,songle@cnap.com.cn,suoya@cnap.com.cn,zhuangjiantao@cnap.com.cn,liuys@263.net";
    public const string postusers = "gejiangjing@autocarcn.com,liuli@autocarcn.com,suoya@cnap.com.cn,zhanglin@cnap.com.cn,zhuangjiantao@cnap.com.cn,liuys@263.net";
    
    public TCommon()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//

	}

    public static  string GetSendMailList()
    {
        string sSql="select userid from t_user1 where grpcode in (select grpcode from t_user_grantfunc as a "+
                "where funccode='A03') "+
                "union "+
                "select userid from t_user_access ";
        string list = "";
        TSqlDBReader myReader=new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            list += myReader.FDataReader["userid"].ToString();
        }
        return list;
    }

    public static bool mySendMail(string email)
    {
        //准备内容
        string uid = "";
        string bigname = "";
        string sSql = "select uid,bigname from t_user where email='" + email + "'";
        TSqlDBReader oUser = new TSqlDBReader(sSql);
        if (oUser.FDataReader.HasRows)
        {
            oUser.FDataReader.Read();
            uid = oUser.FDataReader.GetValue(0).ToString();
            bigname = oUser.FDataReader.GetValue(1).ToString();
        }
        oUser.Free();

        string HtmlBody = "亲爱的" + bigname + ":<br><br><br>";
        HtmlBody += "请点击下面的链接完成注册：<br>";
        HtmlBody += "<a href=\""+wwwurl+"user/user_validate.aspx?uid=" + uid + "\" target=\"_blank\">"+wwwurl+"user/user_validate.aspx?uid=" + uid + "</a><br><br>";
        HtmlBody += "如果以上链接无法点击，请将上面的地址复制到你的浏览器(如IE)的地址栏进入。<br><br><br>";
        HtmlBody += "(这是一封自动产生的email，请勿回复。)";

        try
        {
            //准备邮件
            //MailAddress fromemail = new MailAddress("liuys@263.net", "刘玉升");
            MailAddress fromemail = new MailAddress("admin@cnap.com.cn", "汽车中国");
            MailAddress toemail = new MailAddress(email);

            MailMessage oMail = new MailMessage(fromemail, toemail);
            oMail.SubjectEncoding = System.Text.Encoding.UTF8;
            oMail.Subject = "请激活你的帐号，完成注册";
            oMail.Priority = MailPriority.Normal;
            oMail.IsBodyHtml = true;
            oMail.BodyEncoding = System.Text.Encoding.UTF8;
            oMail.Body = HtmlBody;
            //发送邮件
            //SmtpClient oSmtp = new SmtpClient("smtp.263.net");
            //oSmtp.Credentials = new System.Net.NetworkCredential("liuys@263.net", "098288");
            SmtpClient oSmtp = new SmtpClient("smtp.cnap.com.cn");
            //oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //oSmtp.UseDefaultCredentials = true;
            oSmtp.Credentials = new System.Net.NetworkCredential("admin@cnap.com.cn", "mala990707");
            oSmtp.Send(oMail);

            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }

    public static bool mySendMail_Reset(string email)
    {
        //准备内容
        string uid = "";
        string bigname = "";
        string sSql = "select uid,bigname from t_user where email='" + email + "'";
        TSqlDBReader oUser = new TSqlDBReader(sSql);
        if (oUser.FDataReader.HasRows)
        {
            oUser.FDataReader.Read();
            uid = oUser.FDataReader.GetValue(0).ToString();
            bigname = oUser.FDataReader.GetValue(1).ToString();
        }
        oUser.Free();

        string HtmlBody = "亲爱的" + bigname + ":<br><br><br>";
        HtmlBody += "请点击下面的链接来重设密码：<br>";
        HtmlBody += "<a href=\""+wwwurl+"user/user_reback.aspx?uid=" + uid + "\" target=\"_blank\">"+wwwurl+"user/user_reback.aspx?uid=" + uid + "</a><br><br>";
        HtmlBody += "如果以上链接无法点击，请将上面的地址复制到你的浏览器(如IE)的地址栏进入。<br><br><br>";
        HtmlBody += "(这是一封自动产生的email，请勿回复。)";

        try
        {
            //准备邮件
            //MailAddress fromemail = new MailAddress("liuys@263.net", "刘玉升");
            MailAddress fromemail = new MailAddress("admin@cnap.com.cn", "汽车中国");
            MailAddress toemail = new MailAddress(email);
            MailMessage oMail = new MailMessage(fromemail, toemail);
            oMail.SubjectEncoding = System.Text.Encoding.UTF8;
            oMail.Subject = "密码重设！";
            oMail.Priority = MailPriority.Normal;
            oMail.IsBodyHtml = true;
            oMail.BodyEncoding = System.Text.Encoding.UTF8;
            oMail.Body = HtmlBody;

            //发送邮件
            //SmtpClient oSmtp = new SmtpClient("smtp.263.net");
            //oSmtp.Credentials = new System.Net.NetworkCredential("liuys@263.net", "098288");
            SmtpClient oSmtp = new SmtpClient("smtp.cnap.com.cn");
            //oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //oSmtp.UseDefaultCredentials = true;
            oSmtp.Credentials = new System.Net.NetworkCredential("admin@cnap.com.cn", "mala990707");
            oSmtp.Send(oMail);

            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    //内容装入组合框
    public static void LoadComBox(string typeid, System.Web.UI.WebControls.DropDownList cbTemp)
    {
        string sSql = "select infoid,infoname from t_sys_info2 where isshow='Y' and typeid='" + typeid + "' order by orderby";

        ListItem ItemNone = new ListItem("", "");
        cbTemp.Items.Add(ItemNone);
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            ListItem Item = new ListItem(myReader.FDataReader.GetValue(1).ToString(), myReader.FDataReader.GetValue(0).ToString());
            cbTemp.Items.Add(Item);
        }
        myReader.Free();
    }

    //设置选择项
    public static void SetCombox(string sText, System.Web.UI.WebControls.DropDownList cbTemp)
    {
        for (int i = 0; i < cbTemp.Items.Count; i++)
        {
            if (cbTemp.Items[i].Value == sText)
            {
                cbTemp.SelectedIndex = i;
                break;
            }
        };
    }


    public static void LoadComBox3(string sSql, DropDownList cbTemp)
    {
        cbTemp.Items.Clear();
        ListItem item = new ListItem("请选择...", "");
        cbTemp.Items.Add(item);
        TSqlDBReader reader = new TSqlDBReader(sSql);
        while (reader.FDataReader.Read())
        {
            ListItem item2 = new ListItem(reader.FDataReader.GetValue(1).ToString(), reader.FDataReader.GetValue(0).ToString());
            cbTemp.Items.Add(item2);
        }
        reader.Free();
    }

 
}
